home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / KILLFF.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  4KB  |  124 lines

  1. /*
  2. ** KILLFF.C - A program was written to strip out all the Form Feeds
  3. ** in text files.
  4. **
  5. ** Public domain by Erik VanRiper, 12/22/91
  6. ** Modified by Bob Stout, 17 Feb 93
  7. **
  8. ** Reads a text file and makes a duplicate with NO Form Feed
  9. ** characters! The default action is to create a duplicate without
  10. ** Form Feeds, then remove the original and rename the dupliicate,
  11. ** although an explicit output file name may be specified.
  12. **
  13. ** Form Feed characters are replaced with newline characters ('\n').
  14. ** Since ANSI mandates that fwrite() will translate newlines when
  15. ** a stream is opened in text (non-binary) mode, these will appear
  16. ** in the ouput file in a format appropriate to the implementation,
  17. ** e.g. CRLF pairs on PC's.
  18. **
  19. ** Usage: KILLFF filename [newname]
  20. */
  21.  
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25.  
  26. #define BSIZ 32768U             /* max size of read/write buffer */
  27.  
  28. main(int argc, char *argv[])
  29. {
  30.    FILE *in, *out;              /* input and output files        */
  31.    char name[80],               /* name of file to be fixed      */
  32.         temp[80],               /* output file name              */
  33.         *buf;                   /* buffer we will use to write   */
  34. /*        *s;                     /* searching pointer             */
  35.    size_t bad,                  /* check to see if write ok      */
  36.           num;                  /* number of bytes read          */
  37.    int retval  = EXIT_SUCCESS,  /* return value                  */
  38.        tmpflag = 0;             /* non-zero if tmpnam() used     */
  39.  
  40.    printf("\nKILL FORM FEEDS by Erik VanRiper & Bob Stout\n\n");
  41.  
  42.    if(argc < 2)                              /* usage info       */
  43.    {
  44.       puts("Usage: KILLFF input_file [output_file]");
  45.       puts("\nIf no output file is given, the input file will "
  46.            "be replaced.");
  47.       return retval;                         /* return to OS     */
  48.    }
  49.  
  50.    strcpy(name,argv[1]);                     /* input filename   */
  51.    if(argc == 3) strcpy(temp,argv[2]);       /* outfile name     */
  52.    else
  53.    {
  54.       tmpnam(temp);
  55.       tmpflag = -1;
  56.    }
  57.  
  58.    if((in = fopen(name,"r")) == NULL)        /* Open in file     */
  59.    {
  60.       printf("\nCan't Open Input File %s",name);
  61.       return (retval = EXIT_FAILURE);        /* return to OS     */
  62.    }
  63.    if((out = fopen(temp,"wt")) == NULL)      /* open out file    */
  64.    {
  65.       printf("\nCan't Open Output File %s",temp);
  66.       fclose(in);                            /* close in file    */
  67.       return (retval = EXIT_FAILURE);        /* return to OS     */
  68.    }
  69.  
  70.    if((buf = malloc(BSIZ)) == NULL)     /* malloc a large buffer */
  71.    {
  72.       printf("\nOut of memory\n");
  73.       return (retval = EXIT_FAILURE);        /* return to OS     */
  74.    }
  75.  
  76.    printf("Input file: %s Output file: %s\n",
  77.       name,tmpflag ? name : temp);
  78.  
  79.    /* read in file while chars to read */
  80.  
  81.    while (0 < (num = fread(buf,sizeof(char),BSIZ,in)))
  82.    {
  83.       size_t i;
  84.  
  85.       for (i = 0; i < num; ++i)              /* look for FF      */
  86.          if ('\f' == buf[i])
  87.             buf[i] = '\n';                  /* change to newline */
  88.  
  89.       bad=fwrite(buf,sizeof(char),num,out);  /* write out buf    */
  90.       if(bad != num)                         /* error            */
  91.       {
  92.          printf("\nCan't Write to %s ", temp);
  93.          retval = EXIT_FAILURE;              /* return to OS     */
  94.          break;
  95.       }
  96.    }
  97.    fclose(in);                               /* close in file    */
  98.    fclose(out);                              /* close out file   */
  99.    free(buf);                                /* free memory      */
  100.    if (tmpflag)
  101.    {
  102.       if (remove(name))
  103.       {
  104.          printf("Can't rename %s\n", name);
  105.          printf("Converted file is named %s\n", temp);
  106.       }
  107.       else
  108.          rename(temp, name);
  109.    }
  110.    printf("\nDone!");                        /* Finished         */
  111.    return retval;                            /* return to OS     */
  112. }
  113. /*
  114.  
  115. List this source file to test this program!
  116.  
  117. New page
  118.  
  119. New page
  120.  
  121. All done
  122.  
  123. */
  124.